/*
 * Copyright (c) 2011 Auzingers.com
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */package com.auzingers.docsorganizer.googleapi.xml;

import java.util.List;

import android.test.AndroidTestCase;

import com.auzingers.docsorganizer.entity.GoogleCollection;
import com.auzingers.docsorganizer.entity.GoogleDocument;
import com.auzingers.docsorganizer.entity.GoogleResult;
import com.auzingers.docsorganizer.util.CollectionUtil;
import com.auzingers.test.util.FileUtil;

/** Unit test for XmlParser class. */
public class TestXmlParser extends AndroidTestCase {

    private XmlParser xmlParser;

    public TestXmlParser() {
        xmlParser = new XmlParser();
    }

    /** Testcase for the parseDocuments method. */
    public void testParseDocuments() throws Exception {
        //read file and parse it
        final String xml = FileUtil.getFileContents("/res/raw/documents1.xml");

        final GoogleResult result = xmlParser.parseGoogleEntries(xml);
        assertNotNull(result.getFeedAuthor());
        assertEquals("docsorganizertest", result.getFeedAuthor().getName());
        assertEquals("docsorganizertest@googlemail.com", result.getFeedAuthor().getEmail());

        final List<GoogleDocument> documents = CollectionUtil.convertList(result.getEntries(),
                GoogleDocument.class);

        //validate results
        assertEquals(2, documents.size());
        final GoogleDocument doc1 = documents.get(0);
        assertEquals("Tabelle 1", doc1.getTitle());
        assertEquals("spreadsheet:0AkoZ3CeNrlNMdFRHa2s2ZVpmNTczMndUWUdFeThyNHc", doc1.getResourceId());
        assertEquals("https://docs.google.com/feeds/id/spreadsheet%3A0AkoZ3CeNrlNMdFRHa2s2ZVpmNTczMndUWUdFeThyNHc",
                doc1.getId());
        assertNotNull(doc1.getAuthor());
        assertEquals("docsorganizer", doc1.getAuthor().getName());
        assertEquals("docsorganizer@googlemail.com", doc1.getAuthor().getEmail());
        assertEquals("spreadsheet", doc1.getCategoryKind());

        final GoogleDocument doc2 = documents.get(1);
        assertEquals("Textdokument 1", doc2.getTitle());
        assertEquals("document:1O9iB4kq-30_YUZTzTiepN7o3pTc1t4tMtna21wQDF-A", doc2.getResourceId());
        assertEquals("https://docs.google.com/feeds/id/document%3A1O9iB4kq-30_YUZTzTiepN7o3pTc1t4tMtna21wQDF-A",
                doc2.getId());
        assertEquals("document", doc2.getCategoryKind());
    }

    public void testParseCollections() throws Exception {
        //read file and parse it
        final String xml = FileUtil.getFileContents("/res/raw/collections1.xml");
        final List<GoogleCollection> collections = CollectionUtil.convertList(
                xmlParser.parseGoogleEntries(xml).getEntries(), GoogleCollection.class);

        //validate results
        assertEquals(3, collections.size());
        final GoogleCollection col1 = collections.get(0);
        assertEquals("Col3", col1.getTitle());
        assertEquals("folder:0B0oZ3CeNrlNMYjMxMTgxNjYtNGE0Yy00Zjc1LTk5M2ItZmQ3MTAyZTk4ZGIy", col1.getResourceId());
        assertEquals(
                "https://docs.google.com/feeds/id/folder%3A0B0oZ3CeNrlNMYjMxMTgxNjYtNGE0Yy00Zjc1LTk5M2ItZmQ3MTAyZTk4ZGIy",
                col1.getId());

        final GoogleCollection col2 = collections.get(1);
        assertEquals("Col2", col2.getTitle());
        assertEquals("folder:0B0oZ3CeNrlNMM2NiNDY1YjctMDQ4MC00M2Y0LTlkOTItNDZlN2ZmYjU4YmIy", col2.getResourceId());
        assertEquals(
                "https://docs.google.com/feeds/id/folder%3A0B0oZ3CeNrlNMM2NiNDY1YjctMDQ4MC00M2Y0LTlkOTItNDZlN2ZmYjU4YmIy",
                col2.getId());

        final GoogleCollection col3 = collections.get(2);
        assertEquals("Col1", col3.getTitle());
        assertEquals("folder:0B0oZ3CeNrlNMNWYxOTE5NTUtMjM4Ny00MmQ0LTk1ZDgtNmJiY2UyZGM0ZDUy", col3.getResourceId());
        assertEquals(
                "https://docs.google.com/feeds/id/folder%3A0B0oZ3CeNrlNMNWYxOTE5NTUtMjM4Ny00MmQ0LTk1ZDgtNmJiY2UyZGM0ZDUy",
                col3.getId());
    }

    /**
     * Tests the parsing of a document which does have a parent collection.
     * @throws Exception on error
     */
    public void testParseDocumentWithParent() throws Exception {
        //read file and parse it
        final String xml = FileUtil.getFileContents("/res/raw/docwithparent.xml");
        final List<GoogleDocument> documents = CollectionUtil.convertList(
                xmlParser.parseGoogleEntries(xml).getEntries(), GoogleDocument.class);

        //validate results
        assertEquals(1, documents.size());
        final GoogleDocument doc1 = documents.get(0);
        assertEquals("Document in Col2", doc1.getTitle());
        assertEquals("https://docs.google.com/feeds/default/private/full/folder%3A"
                + "0B0oZ3CeNrlNMM2NiNDY1YjctMDQ4MC00M2Y0LTlkOTItNDZlN2ZmYjU4YmIy", doc1.getParentRefs().get(0));
    }
}
